package com.nettyProtocol.redisDemo;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.nio.charset.Charset;

/**
 * @author： pang-yun
 * @date： 2021-09-15 16:02
 * 功能目的 ：用于   使用netty  简单模拟  redis
 */

public class RedisByNetty {
    public static void main(String[] args) {
        final byte[] LINE ={13,10};   // 回车以及换行
        NioEventLoopGroup work = new NioEventLoopGroup();

        try {
            ChannelFuture channelFuture = new Bootstrap()
                    .group(work)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)).addLast(new ChannelInboundHandlerAdapter() {
                                @Override
                                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                    ByteBuf byteBuf = ctx.alloc().buffer();
                                    byteBuf.writeBytes("*3".getBytes());
                                    byteBuf.writeBytes(LINE);
                                    byteBuf.writeBytes("$3".getBytes());
                                    byteBuf.writeBytes(LINE);
                                    byteBuf.writeBytes("set".getBytes());
                                    byteBuf.writeBytes(LINE);
                                    byteBuf.writeBytes("$4".getBytes());
                                    byteBuf.writeBytes(LINE);
                                    byteBuf.writeBytes("agea".getBytes());
                                    byteBuf.writeBytes(LINE);
                                    byteBuf.writeBytes("$5".getBytes());
                                    byteBuf.writeBytes(LINE);
                                    byteBuf.writeBytes("panga".getBytes());
                                    byteBuf.writeBytes(LINE);


                                    ctx.writeAndFlush(byteBuf);
                                }

                                @Override
                                public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                    ByteBuf buf = (ByteBuf) msg;
                                    System.out.println(buf.toString(Charset.defaultCharset()));
                                }
                            });

                        }
                    }).connect("127.0.0.1", 6379).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            work.shutdownGracefully();
        }

    }
}
